//
//  HeadView.m
//  苏宁
//
//  Created by mini on 15/7/14.
//  Copyright (c) 2015年 mini. All rights reserved.
//

#import "HeadView.h"
#import "Common.h"
#import "HeadModel.h"
#import "headCell.h"
#import "NearbyController.h"
#import "MoreViewController.h"
#import "QRCodeReaderViewController.h"
#import "HomeViewController.h"
#import "DSZLYViewController.h"
#define totalSection 100
static NSString *const name = @"headCell";
@implementation HeadView

-(instancetype)init
{
    self = [super init];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"HeadView" owner:nil options:nil]lastObject];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat W = self.frame.size.width;
    CGFloat H = self.frame.size.height;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //保证collectionView在最下面
    [self sendSubviewToBack:self.collectionView];
    UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_search_normal"]];
    imgView.frame = CGRectMake(20, 0, 15, 15);
    self.textView.leftView = imgView;
    self.textView.leftViewMode = UITextFieldViewModeAlways;
    UIButton *imgView2 = [[UIButton alloc]init];
    [imgView2 setImage:[UIImage imageNamed:@"homeSearchTool_voiceIcon"] forState:UIControlStateNormal];
    imgView2.frame = CGRectMake(0, 0, 25, 25);
    imgView2.alpha = 1;
//    imgView2.image = [UIImage imageNamed:@"homeSearchTool_voiceIcon"];
    //[self.textView addSubview:imgView2];
    //添加录音事件
    [imgView2 addTarget:self action:@selector(voiced) forControlEvents:UIControlEventTouchUpInside];
    self.textView.rightView = imgView2;
    self.textView.delegate = self;
    self.textView.rightViewMode = UITextFieldViewModeAlways;
    self.textView.placeholder = @"清凉不止一点点";
    self.collectionView.pagingEnabled = YES;
}
-(void)awakeFromNib
{
    [super awakeFromNib];
  //因为已添加过所以默认流式布局
//注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"headCell" bundle:nil] forCellWithReuseIdentifier:name];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:totalSection/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    //添加计时器
    [self addTimer];
    
}

-(NSArray*)total
{
    _total = [NSArray array];
    _total = [HeadModel modelArrayWithFilename:@"head.plist"];
    return _total;
}
-(void)addTimer
{
   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(imageChange) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 100;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.total.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   headCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:name forIndexPath:indexPath];
    [cell setModel:_total[indexPath.item]];
    
    return cell;
}

-(NSIndexPath*)current
{
    //当前位置
    NSIndexPath *path = [[self.collectionView indexPathsForVisibleItems]lastObject];
    //中间段跟当前一样的位置
    NSIndexPath *centerPath = [NSIndexPath indexPathForItem:path.item inSection:totalSection/2];
    //滚动到中间
    [self.collectionView scrollToItemAtIndexPath:centerPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    //返回位置
    return centerPath;
    
}
-(void)imageChange
{
    NSIndexPath *currentPath = [self current];
    //计算下一个
    NSInteger nextItem = currentPath.item + 1;
    NSUInteger nextSection = currentPath.section;
    //判断当前是不是最后一个,是的话换到下一段第一个
    if (nextItem == _total.count) {
        nextItem = 0;
        nextSection++;
    }
    //滚动到下一个
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:nextSection] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark  - UICollectionViewDelegate
/**
 *  当用户即将开始拖拽的时候就调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    _timer = nil;
}

/**
 *  当用户停止拖拽的时候就调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat W = self.collectionView.frame.size.width;
    int index =(int)(scrollView.contentOffset.x/W)%6;
    self.whiteView.left = index*(8+3)+243;
}

- (IBAction)saosao {
    
    if ([self.delegate respondsToSelector:@selector(saoma)]) {
        [self.delegate saoma];
    }
    
}



//取消键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textView) {
        [textField resignFirstResponder];
    }
    return YES;
}

-(void)voiced
{
    if ([self.delegate respondsToSelector:@selector(luyin)]) {
        [self.delegate luyin];
    }
}
- (IBAction)fenlei {
    if ([self.delegate respondsToSelector:@selector(pushFenlei)]) {
        [self.delegate pushFenlei];
    }
}
- (IBAction)content {
    if ([self.delegate respondsToSelector:@selector(nearbyPush)]) {
        [self.delegate nearbyPush];
    }
}

- (IBAction)money {
    if ([self.delegate respondsToSelector:@selector(moneyPush)]) {
        [self.delegate moneyPush];
    }
}
- (IBAction)Vip {
    if ([self.delegate respondsToSelector:@selector(yunzuanPush)]) {
        [self.delegate yunzuanPush];
    }
}
- (IBAction)more {
    if ([self.delegate respondsToSelector:@selector(morePush)]) {
        [self.delegate morePush];
    }

}
@end
